### JAVA PARAMETERS - DO NOT CHANGE.
params <- list()
results <- list()
web <- TRUE

params$result<-"results.json"

if(web){
          params$mean<-@@@mean@@@

          params$sessionid <- "@@@sessionid@@@"
          params$taskid <- "@@@taskid@@@"
          params$workspace <- "@@@workspace@@@"
}

if(!web){
          params$mean<-50
          
          params$sessionid <- 123456789
          params$taskid <- 987654321
          params$workspace <- "~/tmp/vaadinR"
}

# Set workspace by session
params$session.workspace <- paste(params$workspace, params$sessionid, params$taskid, sep="/")
if(exists(params$session.workspace) == FALSE){
          dir.create(params$session.workspace, recursive=T)
}
setwd(params$session.workspace)


# Define results targets
results<-list()

results$image$file<-paste("temp.png", sep="")
results$image$title<-"densityplot"

results$txt$file<-"temp.txt"
results$txt$title<-"descriptive statistics on the randomly generated data"

results$table$file <- "temp.tab" 
results$table$title <- "the randomly generated data"

results$sessionid <- params$sessionid
results$dir <- getwd()


## LOAD LIBS
library(lattice)
library(Hmisc)
library(RJSONIO)

### BEGIN SCRIPT
## BUILD DATA
x<-rnorm(100, params$mean,1)

## MAKE PLOT
png(results$image$file, height=200, width=400)
print(densityplot(x))
dev.off()

## PRINT TABLE
x.table <- matrix(x, nrow=10, dimnames=list(paste("row", 1:10, sep=""), paste("col", 1:10, sep="")))
write.table(x=x.table, file=results$table$file)

## WRITE SUMMARY
x.summary <- describe(x)
sink(file=results$txt$file)
print(x.summary)
sink()




## PRINT RESULTS
# PRINT result locations for external loadings.
results.json <- noquote(toJSON(results))
cat(file="results.json", results.json)
